package com.mw.controller;

import com.mw.domain.AjaxResult;
import com.mw.domain.Classgroup;
import com.mw.domain.Speciality;
import com.mw.domain.ClassgroupAndSpeciality;
import com.mw.tool.BeanPropertyRowMapperWithPrefix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@RestController
public class ClassgroupController {
    @Autowired
    private JdbcClient jdbcClient;

    @GetMapping("/classgroups")
    public AjaxResult getAll() {
        String sql = """
            select 
                t.id as t_id, t.name as t_name, t.code as t_code, t.speciality_id as t_specialityId, t.remark as t_remark,
                c.name as c_name, c.remark as c_remark, c.id as c_id
            from classgroup t
            left join speciality c
            on t.speciality_id = c.id
       """;

        /*
         * 借助于自定义的，支持列名添加前缀的BeanPropertyRowMapper
         * 随后可重用这些mapper
         */
        BeanPropertyRowMapper<Classgroup> rowMapper1 = new BeanPropertyRowMapperWithPrefix<>("t_");
        rowMapper1.setMappedClass(Classgroup.class);
        BeanPropertyRowMapper<Speciality> rowMapper2 = new BeanPropertyRowMapperWithPrefix<>("c_");
        rowMapper2.setMappedClass(Speciality.class);

        /* 自定义RowMapper */
        RowMapper<ClassgroupAndSpeciality> rowMapper = new RowMapper<ClassgroupAndSpeciality>() {
            public ClassgroupAndSpeciality mapRow(ResultSet rs, int rowNum) throws SQLException {
                /* 从ResultSet创建多个对象 */
                Classgroup classgroup = rowMapper1.mapRow(rs, rowNum);
                Speciality speciality = rowMapper2.mapRow(rs, rowNum);

                /* 手动组装一个TeacherAndCollege对象 */
                ClassgroupAndSpeciality classgroupAndSpeciality = new ClassgroupAndSpeciality();
                classgroupAndSpeciality.setClassgroup(classgroup);
                classgroupAndSpeciality.setSpeciality(speciality);

                return classgroupAndSpeciality;
            }
        };
        List<ClassgroupAndSpeciality> items =jdbcClient.sql(sql)
                .query(rowMapper)
                .list();

        return AjaxResult.success().selectResult(items.size(), items);
    }

    @GetMapping("/classgroups/{id}")
    public AjaxResult getById(@PathVariable("id") int expectedId) {
        String sql = """
         select 
                t.id as t_id, t.name as t_name, t.code as t_code, t.speciality_id as t_specialityId, t.remark as t_remark,
                c.name as c_name, c.remark as c_remark, c.id as c_id
            from classgroup t
            left join speciality c
            on t.speciality_id = c.id
            where t.id = :id
         """;

        BeanPropertyRowMapper<Classgroup> rowMapper1 = new BeanPropertyRowMapperWithPrefix<Classgroup>("t_");
        rowMapper1.setMappedClass(Classgroup.class);
        BeanPropertyRowMapper<Speciality> rowMapper2 = new BeanPropertyRowMapperWithPrefix<Speciality>("c_");
        rowMapper2.setMappedClass(Speciality.class);

        /* 自定义RowMapper */
        RowMapper<ClassgroupAndSpeciality> rowMapper = new RowMapper<ClassgroupAndSpeciality>() {
            public ClassgroupAndSpeciality mapRow(ResultSet rs, int rowNum) throws SQLException {
                /* 从ResultSet创建多个对象 */
                Classgroup classgroup = rowMapper1.mapRow(rs, rowNum);
                Speciality speciality = rowMapper2.mapRow(rs, rowNum);

                /* 手动组装一个TeacherAndCollege对象 */
                ClassgroupAndSpeciality classgroupAndSpeciality = new ClassgroupAndSpeciality();
                classgroupAndSpeciality.setClassgroup(classgroup);
                classgroupAndSpeciality.setSpeciality(speciality);

                return classgroupAndSpeciality;
            }
        };
        List<ClassgroupAndSpeciality> items =jdbcClient.sql(sql)
                .param("id", expectedId)
                .query(rowMapper)
                .list();

        return AjaxResult.success().selectResult(items.size(), items);
    }

    @PutMapping("/classgroups")
    public AjaxResult update(@RequestBody Classgroup classgroup) {
        String sql = """
        update classgroup set 
            name = :name, 
            code = :code, 
            speciality_id = :specialityId, 
            remark = :remark 
        where id = :id
    """;

        int n = jdbcClient.sql(sql)
                .paramSource(classgroup)
                .update();

        return AjaxResult.success().updateResult(n);
    }

    @DeleteMapping("/classgroups/{id}")
    public AjaxResult deleteById(@PathVariable("id") int expectedId) {
        String sql = "delete from classgroup where id = :id";

        int n = jdbcClient.sql(sql)
                .param("id", expectedId)
                .update();

        return AjaxResult.success().deleteResult(n);
    }

    @PostMapping("/classgroups")
    public AjaxResult insert(@RequestBody Classgroup classgroup) {
        /* 注意college_id列，对应collegeId属性 */
        String sql = """
            insert into classgroup
                (name, code, remark, speciality_id) 
            values
                (:name, :code, :remark, :specialityId)
        """;

        int n = jdbcClient.sql(sql)
                .paramSource(classgroup)
                .update();

        return AjaxResult.success().insertResult(n);
    }
}
